function hyp_synch()
%
% analyze hyp_synch data.
% data consists of a prepulse, followed by a depolarizaiton with or without
% a smaller hyperpolarization.
% this routine averages the response from the end of the first hyperpolarization through about 
% 60 msec later, for comparison between the traces with and traces without the "ipsp".
% Assumes data organization is "alternating", such that successive entries in the
% control are paired, with and without the pulse.
global CONTROL
global PAIR

PAIR = 0;
try
   sf = getmainselection;
   if(sf > 0) 
      pflag = getplotflag;
      QueMessage('hyp_synch analysis', 1); % clear the que
      for i = 1:length(sf)
         hyp_synch_2(sf(i),pflag, i);
      end;
   end;
catch
   watchoff;
   QueMessage('Error in hyp_synch Analysis routine', 1);
end;


function hyp_synch_2(sf, plot_flag, nth)

global DFILE CONTROL PAIR VOLTAGE
persistent avg1 avg2

err = 0;

dat = [];
time = [];
ivresult = []; % initialize it
do_spike = 1;
% various time definitions:
t_pre = 40; % Time to compute average before a spike; normally 25 msec
t_spec_w = 5; % latest time before spike to measure spectrum (e.g., before foot)
t_post = 15;  % window to display AFTER the spike to show ahp
t_delay = 15; % time after last step before the spike before we consider the data on the rising phase valid

min_isi = 2;

[DFILE, err] = analysis_setup(DFILE, sf);

if(err ~= 0)
   return;
end;

% abstract general information
spike_thresh=number_arg(CONTROL(sf).thresh);

% information we need to extract
time=make_time(DFILE);
tmax=max(max(time));
RATES = (DFILE.rate .* DFILE.nr_channel) / 1000; % array of sampling rates, convert to msec

tx1 = 10+CONTROL(sf).durho + CONTROL(sf).durs1;
tx2 = tx1 + 70;
itx = find_times([tx1 tx2]);
switch PAIR
case 0
   avg1 = mean(VOLTAGE(:,[itx(1):itx(2)]));
   avg2 = [];
   PAIR = 1;
case 1
   avg2 = mean(VOLTAGE(:,[itx(1):itx(2)]));
   PAIR = 0;
otherwise
end;

if plot_flag>=0 & ~isempty(avg2)
   fsize = 7;
   msize = 3;
   msg_pos = [0.37 0.00 0.15 0.07];
   
   h = findobj('Tag', 'hyp_synch'); % check for pre-existing window
   if(isempty(h)) % if none, make one
      h = figure('Tag', 'hyp_synch', ...
         'Units', 'normalized', ...
         'MenuBar', 'none', ...
         'WindowButtonMotionFcn', 'datac(''mouse_motion'', gcbf);', ...
         'WindowButtonDownFcn', 'datac(''mouse_down'', gcbf);', ...
         'WindowButtonUpFcn', 'datac(''mouse_up'', gcbf);', ...
         'NumberTitle', 'off');
   end
   figure(h); % otherwise, select it
   clf; % always clear the window...
   %Command Menu
   uimenu('Label', 'Close &Window', 'Position', 1, 'callback', 'close(findobj(''Tag'', ''Forward2''));' );
   uimenu('Label', '&Print', 'Callback', 'print;');
   uimenu('Label', 'Print&Setup', 'Callback', 'printdlg;');
   set(h, 'Name', sprintf('hyp_synch Analysis - File: %s', CONTROL(sf).filename))
   
   
   h1=subplot('Position', [0.1 0.1 0.8 0.7]);
   plot(time(1,itx(1):itx(2))', avg1, 'r');
   hold on;
   plot(time(1,itx(1):itx(2))', avg2, 'b');
   set(gca, 'FontSize', fsize);
   ylabel(CONTROL(sf).V_Unit);
   ylim('auto');
   xlim('auto');
   datac_setcrosshair(gca, 'hyp_synch_info', 'T (ms)', 'Vm (mV)', msg_pos);  
end;
